import java.io.*;

public class pir_sequencer {
 /**
  *  So this thing is gonna read in the input file, 
  *  Parse that input file into an array of sequences
  *  then whenever it gets an input, it'll fire that through
  *  the seqnence clases to see if we get matches. Those
  *  sequence classes will have backreferences to this, so 
  *  they can call the fire, which will backreference pir_io
  *  so that this can call fire there too. 
  */
   private sequence[] my_sequences;
   private int sequence_count; 
   private pir_io pir_io_ref;
	
	public pir_sequencer(String sequence_file_name, pir_io pir_io_ref_passed){
		pir_io_ref = pir_io_ref_passed;
		try{
			my_sequences = new sequence[32];
			BufferedReader  sequence_file_reader = new BufferedReader(new FileReader(sequence_file_name));
			int i = 0;
			while(sequence_file_reader.ready()){
				my_sequences[i]= new sequence(sequence_file_reader.readLine(), pir_io_ref);
				my_sequences[i].start();
			    i++;
			}
			sequence_count = i;
		}catch (Exception e){
			System.out.println("Sequence file " + sequence_file_name + " Not found");
			System.out.println("Or ... " + e);
			return; 
		}
		
		  
	}
	
	/**
	 *  count the sequences
	 * @return count of all sequences
	 */
    public int count(){
    	return sequence_count;
    }
    
    /**
     * Pass an input to all sequences for parsing
     * 
     * @param sensor input sensor triggered
     */
    public void pass(int sensor){
    	for (int i = 0; i < count(); i++){
    		if (my_sequences[i].input(sensor) != 0){
    			my_sequences[i].interrupt();
    		}
    	}
    }
    
    
    public void createAndTrigger(int effect, String sequence_string){
    	sequence immediate_sequence = new sequence(sequence_string, pir_io_ref);
    	immediate_sequence.curstart = effect;
    	immediate_sequence.arm();
    	immediate_sequence.start();
    	immediate_sequence.interrupt();
    	
    }
}
